/**
 * @file Define the marketing handlersPath
 *
 * @author Steve Gao<gaofu@parllay.com>
 */

const WebBaseController = require('./web-base-controller');
const {SystemServices}  = require('../../constants');

/**
 * The MarketingController class
 * 
 * @class MarketingController
 * @extends {BaseController}
 */
class MarketingController extends WebBaseController {

    get wallService() {
        if (!this._wallService) {
            this._wallService = this.getSystemService(SystemServices.WALL_SERVICE);
        }
        return this._wallService;
    }

    get wechatService() {
        if (!this._wechatService) {
            this._wechatService = this.getSystemService(SystemServices.WECHAT_SERVICE);
        }

        return this._wechatService;
    }

    get crawlerService() {
        if (!this._crawlerService) {
            this._crawlerService = this.getSystemService(SystemServices.CRAWLER_SERVICE);
        }

        return this._crawlerService;
    }

    get name() {
        return 'MarketingController';
    }

    get handlersPath() {
        return 'marketing';
    }

    /**
     * Wrap the Wall Service's saveDemoRequest method
     * 
     * @name saveDemoRequest
     * @param {Object} data - The request information
     * @param {Function} parentCallback - The callback function
     * 
     * @returns {void}
     */
    saveDemoRequest(data, parentCallback) {
        this.wallService.saveDemoRequest(data, (result) => {
            parentCallback(result);
        });
    }

    /**
     * Wrap the wall service's createTrialUser method
     * 
     * @name createTrialUser
     * @param {String} host - request host name
     * @param {Object} data - The trial user information
     * @param {Function} parentCallback - The callback function
     * 
     * @returns {void}
     */
    createTrialUser(host, data, parentCallback) {
        this.wallService.createTrialUser(host, data, (result) => {
            parentCallback(result);
        });
    }

    /**
     * Wrap the WallService's getWallById method
     * 
     * @name getWallById
     * @param {String} wallId - The wall id
     * @param {Function} parentCallback - The callback function
     * 
     * @returns {void}
     */
    getWallById(wallId, parentCallback) {
        this.wallService.getWallById(wallId, (data) => {
            parentCallback(data);
        });
    }

    /**
     * Get WeChat Jssdk config, wrap the WeChatService's getWeChatJssdkConfig method
     */
    getWeChatJssdkConfig(appId, request, parentCallback) {
        this.wechatService.getWeChatJssdkConfig(appId, request, (config) => {
            parentCallback(config);
        });
    }

    getAssetStatisticsById(assetId, parentCallback) {
        this.wallService.getAssetStatisticsById(assetId, (result) => {
            parentCallback(result);
        });
    }

    /**
     * Ensure the system generate an anonymous user, wrap the WallService's ensureAnonymousUser
     */
    //TODO: Need refactor
    ensureAnonymousUser(request, wallUserId, parentCallback) {
        this.wallService.ensureAnonymousUser(request, wallUserId, (userId) => {
            parentCallback(userId);
        });
    }

    updateAssetStatistics(assetId, wallUserId, action, data, parentCallback) {
        this.wallService.updateAssetStatistics(assetId, wallUserId, action, data, (result) => {
            parentCallback(result);
        });
    }

    /**
     * Fetch content from url
     * 
     * @name fetchContent
     * @param {String} url - the url to fetch 
     * @param {Function} parentCallback - The callback function
     */
    fetchContent(url, parentCallback) {
        this.crawlerService.fetchContent(url, (content) => {
            parentCallback(content);
        });
    }
}

exports.MarketingController = MarketingController;